package com.wei.wiki.controller;

/*
 * @author : ShunWei
 * @date : 2022/1/27 17:20
 * @Description:
 */

import com.alibaba.fastjson.JSONObject;
import com.wei.wiki.req.UserLoginReq;
import com.wei.wiki.req.UserQueryReq;
import com.wei.wiki.req.UserSaveReq;
import com.wei.wiki.resp.CommonResp;
import com.wei.wiki.resp.PageResp;
import com.wei.wiki.resp.UserLoginResp;
import com.wei.wiki.resp.UserQueryResp;
import com.wei.wiki.service.UserService;
import com.wei.wiki.util.SnowFlake;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/user")
@Api(tags = "UserController", description = "用户控制类")
public class UserController {
    private UserService userService;

    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private SnowFlake snowFlake;

    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    @GetMapping("/list")
    @ApiOperation("用户信息列表")
    public CommonResp getUserList(@Valid UserQueryReq req) {
        CommonResp<PageResp<UserQueryResp>> resp = new CommonResp<>();
        PageResp<UserQueryResp> bookList = userService.bookList(req);
        resp.setContent(bookList);
        return resp;
    }

    @PostMapping("/save")
    @ApiOperation("新增、修改用户")
    public CommonResp save(@RequestBody @ApiParam("用户对象") UserSaveReq req) {
        // 密码md5加密
        req.setPassword(DigestUtils.md5DigestAsHex(req.getPassword().getBytes()));
        CommonResp resp = new CommonResp<>();
        userService.save(req);
        return resp;
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation("根据id删除用户")
    public CommonResp delete(@PathVariable Long id) {
        CommonResp resp = new CommonResp<>();
        userService.delete(id);
        return resp;
    }

    @PostMapping("/reset-password")
    @ApiOperation("重置密码")
    public CommonResp resetPassword(@RequestBody @ApiParam("用户对象") UserSaveReq req) {
        // 密码md5加密
        req.setPassword(DigestUtils.md5DigestAsHex(req.getPassword().getBytes()));
        CommonResp resp = new CommonResp<>();
        userService.resetPassword(req);
        return resp;
    }

    @PostMapping("/login")
    @ApiOperation("用户登录")
    public CommonResp login(@RequestBody @ApiParam("用户对象") UserLoginReq req) {
        req.setPassword(DigestUtils.md5DigestAsHex(req.getPassword().getBytes()));
        CommonResp<UserLoginResp> resp = new CommonResp<>();
        UserLoginResp userLoginResp = userService.login(req);
        // 生成单点token 放入redis
        Long token = snowFlake.nextId();
        userLoginResp.setToken(token.toString());
        redisTemplate.opsForValue().set(token.toString(), JSONObject.toJSONString(userLoginResp), 3600 * 24, TimeUnit.SECONDS);
        resp.setContent(userLoginResp);
        resp.setMessage("登录成功！");
        return resp;
    }

    @ApiOperation("注销")
    @GetMapping("logout/{token}")
    public CommonResp logout(@PathVariable @ApiParam("token") String token) {
        CommonResp resp = new CommonResp<>();
        redisTemplate.delete(token);
        return resp;
    }
}
